#include "stdafx.h"
#pragma once
//-------------------------------------------------------------------------------------------------
//ImageDataset.h  - Declaration of Image Dataset           
//-------------------------------------------------------------------------------------------------
//FreeBSD License
//Copyright 2011 Will Stott All rights reserved.
//
//Redistribution and use in source and binary forms, with or without modification, are
//permitted provided that the following conditions are met:
//
//   1. Redistributions of source code must retain the above copyright notice, this list of
//      conditions and the following disclaimer.
//
//   2. Redistributions in binary form must reproduce the above copyright notice, this list
//      of conditions and the following disclaimer in the documentation and/or other materials
//      provided with the distribution.
//
//THIS SOFTWARE IS PROVIDED BY Will Stott ``AS IS'' AND ANY EXPRESS OR IMPLIED
//WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
//FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL Will Stott OR
//CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
//CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
//SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
//ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
//ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//The views and conclusions contained in the software and documentation are those of the
//authors and should not be interpreted as representing official policies, either expressed
//or implied, of Will Stott.
//
//----Authors--------------------------------------------------------------------------------------
//created by: will.stott@ucl.ac.uk Feb 2011
//
//----Comments-------------------------------------------------------------------------------------
//
//-------------------------------------------------------------------------------------------------
#ifdef IMAGEPROCLIB_EXPORTS
#define IMAGEPROCLIBAPI __declspec(dllexport)
#else
#define IMAGEPROCLIBAPI __declspec(dllimport)
#endif

#include <vector>
#include <string>
#include "ResourceList.h"

#include "ImageType.h"
#include "ImageObjectType.h"

#include "ImagePt.h"
#include "Boundary.h"
#include "ImageProps.h"
#include "TinyXmlLib.h"

const int _TEMPBUFFLEN=512;

class IMAGEPROCLIBAPI ImageDataset 
{
public:
	static const char * const xmlHeader;
	static const char * const xmlHeaderOwner;
	static const char * const xmlHeaderName;
	static const char * const xmlHeaderDescription;
	static const char * const xmlHeaderRepository;
	static const char * const xmlHeaderVersion;
	static const char * const xmlHeaderRuntimeDir;
	static const char * const xmlHeaderDataQueryFile;

	static const char * const xmlBody;
	static const char * const xmlImages;

private:
	TiXmlDocument *_pDoc;
	TiXmlElement  *_pHeader;
	TiXmlElement  *_pBody;
	TiXmlElement  *_pImages;

//#pragma warning(disable:4251)
	std::vector<ImageType *>	   _ImageTypes;							//types of images that may be found in this dataset
//#pragma warning(default:4251)
	ImageType	  *_pImageTypeUnclassified;								//set in InitImageTypes and get in GetImageType if input ImageTypeName is not known

	char		  *_pTempBuffer;
	size_t		   _tempBuffLen;

	size_t		   _ImageTypeIter;
	int		   _ImageObjectTypeIter;
	std::string _IDFPathFileName;
	std::string _SelImagePathFileName;

public:
	ImageDataset(void);
	ImageDataset::ImageDataset(const TCHAR *documentName, const char *documentTitle, const char *TopNodeName);
	virtual ~ImageDataset(void);

	bool			CreateHeader();
	bool			CreateBody();

	bool			 IsValidIDFFile() { return ((_IDFPathFileName.length() > 1) ? true : false); }
	const char *	 GetIDFPathFileName() { return _IDFPathFileName.c_str(); }
	const char *	 GetSelImagePathFileName() { return _SelImagePathFileName.c_str(); }
	void			 SetSelImagePathFileName(const char *DicomRef);
	bool			 IsValidBody() { return (((_pImages != '\0') && (_pTempBuffer != '\0')) ? true : false); }
	bool			 IsValidHeader() { return (((_pHeader != '\0') && (_pTempBuffer != '\0')) ? true : false); }
	const char *	 GetHeaderValue(const char *Label);
	bool			 SetHeaderValue(const char *Label, const char *Value);

	const char *     GetImageTypeName(TiXmlElement *pImageXml );
	ImageType       *GetImageType(const char *name);
	bool			 AddImageType (ImageType *pImageType );
	ImageType		*LookupImageType (const char *name);
	int				 GetImageTypeCnt() { return _ImageTypes.size(); }
	ImageType		*GetImageType(int index) { return ((index >= 0) && (index <  GetImageTypeCnt())) ?_ImageTypes[index] : '\0'; }

	ImageObjectType *StartImageObjectTypeIter();
	ImageObjectType *GetNextImageObjectType();

	ImageObjectType *GetImageObjectTypeForMenuId( int Id);
	ImageObjectType *LookupImageObjectType (const char *name);
	
	bool			 SaveFile(const TCHAR *idfPathFileName);
	bool			 LoadFile(const TCHAR *idfPathFileName, ResourceList &resourceList);
	bool			 LoadPropertiesForImage(const char *DicomRef, ImageProps *pSelImageProps, ImageObjectTypeTool *pSelTool);
	bool			 SavePropertiesForImage(ImageProps *pSelImageProps);
	
	bool			 AddImage(const TCHAR *dicomRef);
	
	TiXmlElement *		IterateImages(TiXmlElement *child);
	const char *		GetRuntimeDir();
	const char *		GetSelImagePathfilename();

	static	bool		SetXmlAttribute(TiXmlElement *pXmlElement, const char *Name, const char *Value);
	static	const char *GetXmlAttribute(TiXmlElement *pXmlElement, const char *Name, const char *defaultVal);
	static  const char *MakeWellFormedAttributeName(std::string &AttribName);
	static	bool	    DeleteAllSubSections (TiXmlElement *pSectionXml, const char *subSectionName );

private:
	bool			ClearAllLists();
	void			InitImageTypes(ResourceList &resourceList);

};


//-------------------------------------------------------------------------------------------------
//			E N D   O F   F I L E
//-------------------------------------------------------------------------------------------------

